import openpyxl
import pandas as pd
import requests
from bs4 import BeautifulSoup
# 读取网址文件
with open('urls2.txt', 'r',encoding="utf-8") as file:
    urls = file.readlines()
# 遍历每个网址
n1 = urls[0]
n2 = urls[1]
i = 0
for i, url in enumerate(urls):
    url = url.strip()  # 去除换行符
    print(f'url {i+1}: {url}')
    if i >= 2:
        break
o=pd.read_html(n1)
r= requests.get(n2)
soup=BeautifulSoup(r.text,"html.parser")#转变为response对象
wb=soup.find("title").string#获取对应元素内容
print(wb)
#.string#获取文本
wb=str(wb)#文本转化为字符串
f=[]#将字符串转化为列表
f.append(wb)#如上
wb=pd.DataFrame([wb])

    # 发送HTTP请求获取页面内容
    #response = requests.get(url)
    #if response.status_code == 200:
        # 使用BeautifulSoup解析页面内容
        #soup = BeautifulSoup(response.content,'html.parser')
        # 例如，这里从网页中提取标题和内容
        #b = soup.find_all("div",class_="tanking-table")
        #for i in b:
            #text = soup.find_all("td")
            #print(text)
            #for line in urls:
                #if line == urls[0]:
                    #n1.append(text)
                #if line == urls[1]:
                    #n2.extend(text)
        # 构建DataFrame保存数据
        #df1 = pd.DataFrame(n1,columns=['学校名称'])
        #df2 = pd.DataFrame(n2,columns=['国家'])
        # 将数据写入Excel的不同sheet中并保存Excel文件
    #with pd.ExcelWriter('output3.xlsx', engine='openpyxl') as writer:
o[0].to_excel('output3.xlsx', sheet_name='1',index=False)
            #df2.to_excel(writer, sheet_name=f'Sheet_name=2', index=False)
book =openpyxl.load_workbook('output3.xlsx')#打开上述excel
writer=pd.ExcelWriter('output3.xlsx')#创建新的工作对象，保证可以不覆盖写入
writer._book = book#将原来的数据保存
wb.to_excel(writer, sheet_name='2', index=False)#写入第二个元素并保存至第二个工作表
writer._save()#保存文件
print('数据保存完成')
#将列表转化为dataf